package operation;

import abbot.finder.BasicFinder;
import abbot.finder.ComponentNotFoundException;
import abbot.finder.MultipleComponentsFoundException;
import abbot.finder.matchers.ClassMatcher;
import abbot.finder.matchers.WindowMatcher;
import abbot.tester.WindowTester;
import com.supermap.desktop.core.progress.Interface.IUpdateProgress;
import robotCross.MultipleWindowMacher;
import util.Info;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.TimeoutException;

public class DialogOperation extends Operation {

	//构造函数初始化当前操作的控件容器
	public DialogOperation() {
		if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow() instanceof JDialog) {
			container = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
		}
	}

	public DialogOperation(JDialog dialog){
		this.container = dialog;
	}

	public DialogOperation(String title) {
		this();
		if (title != null) {
			try {
				container = (JDialog) new BasicFinder().find(new WindowMatcher(title));
			} catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
				// TODO Auto-generated catch block
				if (e instanceof MultipleComponentsFoundException) {
					Component[] components = ((MultipleComponentsFoundException) e).getComponents();
					for (Component component : components) {
						if (component instanceof JDialog) {
							container = (JDialog) component;
						}
					}
				}
				e.printStackTrace();
			}
		}

	}

	@Override
	public Object getComponentValue(Component component, String valueDescription) {
		return super.getComponentValue(component, valueDescription);
	}

	public JDialog findDialog(String title) {
		JDialog dialog = null;
		if (title != null) {
			try {
				dialog = (JDialog) new BasicFinder().find(new WindowMatcher(title));
			} catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
				// TODO Auto-generated catch block
				if (e instanceof MultipleComponentsFoundException) {
					Component[] components = ((MultipleComponentsFoundException) e).getComponents();
					for (Component component : components) {
						if (component instanceof JDialog) {
							dialog = (JDialog) component;
							return dialog;
						}
					}
				}
				e.printStackTrace();
			}
		} else {
			if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow() instanceof JDialog) {
				dialog = (JDialog) KeyboardFocusManager
						.getCurrentKeyboardFocusManager().getFocusedWindow();
			}
		}
		return dialog;
	}

	/**
	 * 查找进度条dialog。
	 */
	public JDialog findProgressDialog() {
		JDialog dialog = null;
		try {
			dialog = (JDialog) new BasicFinder().find(new ClassMatcher(IUpdateProgress.class, true));
		} catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
//			e.printStackTrace();
		}
		return dialog;
	}

	public Object closeWindow() {
		if (container != null) {
			new WindowTester().actionClose(container);//点击对话框右上角的关闭符号，并不能满足所有情况。
		}
		return container;
	}

	public JDialog closeWindow(String title) {
		JDialog dialog = this.findDialog(title);
		Operation.robot.delay(1000);
		if (dialog != null) {
			new WindowTester().actionClose(dialog);//点击对话框右上角的关闭符号，并不能满足所有情况。
		}
		return dialog;
	}


	/**
	 * 查找所有对话框
	 */

	public ArrayList<Component> findAllDialog() {
		ArrayList<Component> dialogs = new ArrayList<>();
		try {
			Component component = new BasicFinder().find(new MultipleWindowMacher("/.*/", true));
			dialogs.add(component);
		} catch (ComponentNotFoundException | MultipleComponentsFoundException e) {
			// TODO Auto-generated catch block
			if (e instanceof MultipleComponentsFoundException) {
				Component[] components = ((MultipleComponentsFoundException) e).getComponents();
				Collections.addAll(dialogs, components);
			}
			e.printStackTrace();
		}
		return dialogs;
	}

	/**
	 * 关闭所有对话框
	 */
	public static void closeAllDialog() {
		Info.out("try to close all");
		JDialog dialog;
		do {
			dialog = new DialogOperation().closeWindow(null);
		} while (dialog != null);
		ArrayList<Component> dialogs = new DialogOperation().findAllDialog();
		for (Component component : dialogs) {
			if (component instanceof com.supermap.desktop.controls.ui.MainFrame) continue;
			JDialog dialog1 = (JDialog) component;
			Info.out("dialog title:" + dialog1.getTitle());
			try {
				new WindowTester().actionClose(dialog1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

		/*list 操作*//*
		if (c instanceof JList && cmd.getValues() != null
				&& cmd.getValues().length > 0) {
			ActionUtil.selectListObject((JList) c, cmd.getValues());
			
		} else if (cmd.getAction() == null || cmd.getAction().length == 0) {
			robot.click(c);
		}
		return null;*/



	/**
	 * timeOut:能都容忍的最大执行时间，单位为毫秒,超出最大时间窗口还在，则会抛异常
	 * Author:caolp
	 */
	public void WaitForWindowDispose(long timeOut) {
		long startTime = System.currentTimeMillis();
		while (container != null && container.isVisible()) {
			long endTime = System.currentTimeMillis();
			long totle = endTime - startTime;
			if (totle > timeOut) {
				try {
					throw new TimeoutException("执行时间总计为:" + totle + ",大于最大允许时间，可能是运行环境出问题或是该功能性能降低");
				} catch (TimeoutException e) {
					e.printStackTrace();
				}
				break;
			}
		}
		if (container == null) {
			System.out.println("窗口已释放！");
		}
	}
}
